<?php
/*
File     :	
wpmu-rss.php
Created  :	23/02/2007 15.55
Version  :	0.9.0
Author   :	hery@serasera.org
URI      :	http://hery.blaogy.org/2007/02/23/wpmu-rss-feed/
Comments :	Just some attempts to have RSS Feed for wpmu based blog
Features :	- It uses the wp cache system
			- Can be configured
			- The very first article for each blog (usually the Hello message)
			will not be shown

Installation:
			Just put it in your root and call it with http://yourblogdomain.com/wpmu-rss.php

Credits  :	The function is adapted from the ah_recent_posts
			Plugin URI: http://atypicalhomeschool.net/wordpress-plugins/
			Otherwise it is just wp-rss.php


*/

//configuration here

define("WPMU_RSS_LIMIT", 10); //10 entries
define("WPMU_RSS_CACHETIME", 600); //600 seconds




function ah_recent_posts_mu_mod($how_many = 10, $cachetime = 600) {
	global $wpdb;
	$counter = 0;
	// get a list of blogs in order of most recent update


		if ( ! ( $ah_recent_posts_mu = wp_cache_get('ah_recent_posts_mu' . $how_many, 'pages') ) ) {
			// not in the page cache?
			
			$blogs = $wpdb->get_col("SELECT blog_id FROM $wpdb->blogs ORDER BY last_updated DESC LIMIT $how_many");

			if ($blogs) {
				foreach ($blogs as $blog) {
					// we need _posts and _options tables for this to work
						$blogPostsTable = "wp_".$blog."_posts";

					if($ah_recent_posts = $wpdb->get_results("SELECT * 
						FROM $blogPostsTable WHERE ID > 1 AND  post_status = 'publish' 
						AND post_type = 'post' AND post_password = '' ORDER BY id DESC LIMIT 0,1")) {
						$ah_recent_posts[0]->post_author_name = get_author_name($ah_recent_posts[0]->post_author);
						$ah_recent_posts[0]->post_summary = substr(strip_tags(str_replace("<!--more-->", "", $ah_recent_posts[0]->post_content)), 0, 200);
						//$ah_recent_posts[0]->post_cat_name = get_cat_name($ah_recent_posts[0]->post_category);

						$ah_recent_posts_mu[] = $ah_recent_posts[0];
					}

				}
			}

			wp_cache_set('ah_recent_posts_mu' . $how_many, $ah_recent_posts_mu, 'pages', $cachetime);
		}

	Return $ah_recent_posts_mu;
}


if (empty($wp)) {
	require_once('wp-config.php');
	wp('feed=rss');
}

header('Content-type: text/xml; charset=' . get_option('blog_charset'), true);
$more = 1;

?>
<?php echo '<?xml version="1.0" encoding="'.get_option('blog_charset').'"?'.'>'; ?>
<!-- generator="wordpress/<?php echo $wp_version ?>" -->
<rss version="0.92">
<channel>
	<title><?php bloginfo_rss('name') ?></title>
	<link><?php bloginfo_rss('url') ?></link>
	<description><?php bloginfo_rss('description') ?></description>
	<lastBuildDate><?php echo mysql2date('D, d M Y H:i:s +0000', get_lastpostmodified('GMT'), false); ?></lastBuildDate>
	<docs>http://backend.userland.com/rss092</docs>
	<language><?php echo get_option('rss_language'); ?></language>
<?php if($all_posts = ah_recent_posts_mu_mod(WPMU_RSS_LIMIT, WPMU_RSS_CACHETIME)) : ?>
<?php foreach($all_posts as $all_post): ?>

	<item>
		<title><?php echo htmlspecialchars($all_post->post_title) ?></title>
		<description><?php echo htmlspecialchars($all_post->post_content) ?></description>
		<author><?php echo $all_post->post_author_name ?></author>
		<link><?php echo $all_post->guid ?></link>
	</item>
<?php endforeach; ?>
<?php endif; ?>
</channel>
</rss>